//
//  SPLoginViewController.m
//  SuperPatrner
//
//  Created by EvanZ on 15/3/15.
//  Copyright (c) 2015年 EvanZ. All rights reserved.
//

#import "SPLoginViewController.h"
#import "SPSignInViewController.h"
#import "UIColor+CreateMethod.h"
#import "SPRetrievePasswordViewController.h"
#import "SPHTTPRequestOperationManager.h"
#import "Methods.h"

@interface SPLoginViewController (){
    UITextField * m_usernameField;
    UITextField * m_passwordField;
    float leftoffset;
}

@end

@implementation SPLoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    leftoffset = 50;
    
    UIImage * bgImage = [UIImage imageNamed:@"login_bg"];
    UIImageView * bgImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.width/bgImage.size.width*bgImage.size.height)];
    [bgImageView setImage:bgImage];
    [self.view addSubview:bgImageView];
    
    UIImageView * iconView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 75, 75)];
    [iconView setImage:[UIImage imageNamed:@"logo_icon"]];
    [iconView setCenter:CGPointMake(bgImageView.center.x, bgImageView.center.y-10)];
    [self.view addSubview:iconView];
    
    UITextField * titleField = [[UITextField alloc]initWithFrame:CGRectMake(80, iconView.frame.origin.y + iconView.frame.size.height + 10, self.view.frame.size.width-60, 30)];
    [titleField setBackgroundColor:[UIColor clearColor]];
    [titleField setText:@"凝聚人才，共享财富"];
    [titleField setTextColor:[UIColor whiteColor]];
    [titleField setFont:[UIFont boldSystemFontOfSize:18]];
    [titleField setEnabled:NO];
    [self.view addSubview:titleField];
    
    m_usernameField = [[UITextField alloc]initWithFrame:CGRectMake(leftoffset, bgImageView.frame.origin.y + bgImageView.frame.size.height + 20, self.view.frame.size.width - 2*leftoffset, 30)];
    [m_usernameField setBackgroundColor:[UIColor clearColor]];
    [m_usernameField setPlaceholder:@"手机号"];
    [self.view addSubview:m_usernameField];
    
    UIImageView * usernameIcon = [[UIImageView alloc]initWithFrame:CGRectMake(20, m_usernameField.frame.origin.y, 20, 20)];
    [usernameIcon setImage:[UIImage imageNamed:@"login_user_icon"]];
    [self.view addSubview:usernameIcon];
    
    m_passwordField = [[UITextField alloc]initWithFrame:CGRectMake(leftoffset, m_usernameField.frame.origin.y + m_usernameField.frame.size.height + 20, self.view.frame.size.width - 2*leftoffset, 30)];
    [m_passwordField setBackgroundColor:[UIColor clearColor]];
    [m_passwordField setPlaceholder:@"密码"];
    [self.view addSubview:m_passwordField];
    
    UIImageView * paswordIcon = [[UIImageView alloc]initWithFrame:CGRectMake(20, m_passwordField.frame.origin.y, 20, 20)];
    [paswordIcon setImage:[UIImage imageNamed:@"login_password_icon"]];
    [self.view addSubview:paswordIcon];
    
    UIButton * loginButton = [[UIButton alloc]initWithFrame:CGRectMake(0, m_passwordField.frame.origin.y + m_passwordField.frame.size.height + 10, self.view.frame.size.width, 40)];
    [loginButton setBackgroundImage:[UIImage imageNamed:@"login_login_btn"] forState:UIControlStateNormal];
    [loginButton setTitle:@"登录" forState:UIControlStateNormal];
    [loginButton addTarget:self action:@selector(loginButtonListener) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:loginButton];
    
    UIButton * forgetPasswordButton = [[UIButton alloc]initWithFrame:CGRectMake(0, loginButton.frame.origin.y + loginButton.frame.size.height + 10, self.view.frame.size.width, 30)];
    [forgetPasswordButton setTitle:@"忘记密码" forState:UIControlStateNormal];
    [forgetPasswordButton setTitleColor:[UIColor colorWith8BitRed:122 green:177 blue:216 alpha:1] forState:UIControlStateNormal];
    [forgetPasswordButton addTarget:self action:@selector(forgetPasswordButtonListener) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:forgetPasswordButton];
    
    UIButton * signinButton = [[UIButton alloc]initWithFrame:CGRectMake(0, forgetPasswordButton.frame.origin.y + forgetPasswordButton.frame.size.height + 40, self.view.frame.size.width, 40)];
    [signinButton setTitle:@"注册超级合伙人" forState:UIControlStateNormal];
    [signinButton setTitleColor:[UIColor colorWith8BitRed:122 green:177 blue:216 alpha:1] forState:UIControlStateNormal];
    [signinButton addTarget:self action:@selector(signinButtonListener) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:signinButton];
    
    UITapGestureRecognizer *tapBg = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(resignTextField)];
    tapBg.cancelsTouchesInView = NO;
    [self.view addGestureRecognizer:tapBg];
}

- (void)resignTextField
{
    [m_usernameField resignFirstResponder];
    [m_passwordField resignFirstResponder];
}

#pragma mark - button listeners
- (void)loginButtonListener{
    if (m_usernameField.text.length == 0) {
        [self showHintProgressWithText:@"请输入用户名"];
    }
    else if (m_passwordField.text.length == 0){
        [self showHintProgressWithText:@"请输入密码"];
    }
    else{
        if ([Methods isExistenceNetwork]) {
            [self login];
        }
        else{
            [self showHintProgressWithText:@"无可用网络"];
        }
    }
}

- (void)signinButtonListener{
    SPSignInViewController * vc = [[SPSignInViewController alloc]init];
    UINavigationController * nav =  [[UINavigationController alloc]initWithRootViewController:vc];
    nav.navigationBarHidden = YES;
    [self.navigationController presentViewController:nav animated:YES completion:nil];
}

- (void)forgetPasswordButtonListener{
    SPRetrievePasswordViewController * vc = [[SPRetrievePasswordViewController alloc]init];
    UINavigationController * nav = [[UINavigationController alloc]initWithRootViewController:vc];
    nav.navigationBarHidden = YES;
    [self.navigationController presentViewController:nav animated:YES completion:nil];
}

- (void)login{
    [self showProgressAutoCloseInNetwork:@"登陆中，请稍候"];
    NSDictionary * para = [[NSDictionary alloc]initWithObjectsAndKeys:m_usernameField.text,@"phone",[Methods md5:m_passwordField.text],@"password", nil];
    SPHTTPRequestOperationManager *manager = [SPHTTPRequestOperationManager manager];
    [manager loginPara:para Success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"login>>>>>>>>SUCCESS: %@\nmessage:%@",responseObject,[responseObject valueForKey:@"message"]);
        //login success
        if ([[responseObject valueForKey:@"is_success"] intValue] != 0) {
            [[NSUserDefaults standardUserDefaults]setValue:[responseObject valueForKey:@"user_id"] forKey:@"user_id"];
            [[NSUserDefaults standardUserDefaults]setValue:[responseObject valueForKey:@"invitation_code"] forKey:@"inviatation_code"];
            [[NSUserDefaults standardUserDefaults] setValue:m_usernameField.text forKey:@"phone"];
            [[NSUserDefaults standardUserDefaults] setValue:m_passwordField.text forKey:@"password"];
            
            [[NSNotificationCenter defaultCenter]postNotificationName:NOTI_LOGIN_SUCC object:nil];
            [self dismissViewControllerAnimated:YES completion:nil];
        }
        //login failed
        else{
            [self closeProgress];
            [self showHintProgressWithText:[responseObject valueForKey:@"message"]];
        }
        
    }failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"login>>>>>>>>Error: %@", error);
        [self closeProgress];
        [self showHintProgressWithText:@"登录失败"];
    }];
}

@end
